home *** CD-ROM | disk | FTP | other *** search
- Path: sci.kun.nl!usenet
- From: Marian Hellema <marian@atcmp.nl>
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Function in private part?
- Date: 8 Feb 1996 11:47:11 GMT
- Organization: AT Computing, Nijmegen, the Netherlands
- Message-ID: <4fcnrv$6q6@wn1.sci.kun.nl>
- References: <4fa52o$pkb@news1.usa.pipeline.com> <4fbat4$o80@qualcomm.com>
- NNTP-Posting-Host: atcmpg.atcmp.kun.nl
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m)
- X-URL: news:4fbat4$o80@qualcomm.com
-
- nabbasi@qualcomm.com (Nasser Abbasi) wrote:
- >In article <4fa52o$pkb@news1.usa.pipeline.com>, grantp@usa.pipeline.co
- >says...
- >>
- >>class A { public:
- >>void Expand { Left = MakeChild(); Right = MakeChild(); }
- >>private:
- >>virtual A * MakeChild() { return new A; }
- >>A * Left; A * Right;
- >>};
- >>
- >>class B {
- >> private:
- >> virtual A* MakeChild() { return new B; }
- >>};
- >>
- >>Now, when you call Expand, the appropriate types of
- >>objects will be constructed.
- >>
- >>
- >
- >In your class B, the MakeChild() is declared to return pointer to A,
- >yet it returns a pointer to B. Do you not need to do type conversion
- >here?
- >
- >Will this compile without errors or at least a warning?
- >
-
- I suppose that class B is meant to be publicly derived from class A.
- In that case, you don't need an explicit conversion, because a
- baseclass-pointer can point to a derived-class object.
-
- By the way, the proposal for the ISO/ANSI standard allows B::MakeChild()
- to return a pointer to B, like this:
-
- class A {
- public:
- void Expand() { Left = MakeChild(); Right = MakeChild(); }
- private:
- virtual A * MakeChild() { return new A; }
- A * Left; A * Right;
- };
-
- class B : public A {
- private:
- virtual B* MakeChild() { return new B; }
- };
-
-
- The restriction is that the returntype in the derived class is
- a pointer or reference to a class that is publicly derived from
- the returntype in the baseclass.
-
- Marian
- --
- ----------------------------------------------------------------------
- Marian Hellema AT Computing, UNIX training and consultancy
- email: marian@atcmp.nl P.O. Box 1428, 6501 BK Nijmegen
- phone: +31 24 3527225 the Netherlands
-
-